﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Common.Entity;
using ESOL.Business.Prod;
using ESOL.Common;
using ESOL.WinUI.Bpt;
using ESOL.WinUI.Proj;

namespace ESOL.WinUI.Manufacture
{
    public partial class frmMaintainWorkingOrderQuery : BaseForm.frmBaseDialogyQueryForm
    {
        #region vars
        public ProdPlanOrderItemEntity SelectedProdPlanOrderItem { get { return mSelectedProdPlanOrderItem; } }
        public ProdPlanOrderEntity ProdPlanOrder { set; get; }
        private ProdPlanOrderItemEntity mSelectedProdPlanOrderItem;
        #endregion

        #region ctors
        public frmMaintainWorkingOrderQuery()
        {
            InitializeComponent();
        }
        #endregion

        #region functions
        private void BindComobox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "POI", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(rcolStatus, "POI", false);
        }

        private void SetField()
        {
            if (ProdPlanOrder != null)
            {
                txtProdPlanOrderCode.Tag = ProdPlanOrder;
                txtProdPlanOrderCode.Text = ProdPlanOrder.PlanOrderCode;
            }
        }

        private string GetCondition()
        {
            string condition = "";
            if (txtProdPlanOrderCode.Tag != null && txtProdPlanOrderCode.Tag.GetType() == typeof(ProdPlanOrderEntity))
            {
                ProdPlanOrderEntity mProdPlanOrder = (ProdPlanOrderEntity)txtProdPlanOrderCode.Tag;
                condition += " AND ppo.PLAN_ORDER_ID = '" + mProdPlanOrder.PlanOrderId + "' ";
            }
            if (!string.IsNullOrEmpty(txtPlanOrderItem.Text))
            {
                condition += " AND ppoi.ORDER_ITEM_CODE LIKE '%" + txtPlanOrderItem.Text + "%' ";
            }
            if (dtpDeliveryDate.EditValue!=null)
            {
                condition += " AND ppoi.DELIVERY_DATE>='"+((DateTime)dtpDeliveryDate.EditValue).ToString("yyyy-MM-dd")+" 00:00:00' ";
            }
            if (dtpDeliveryDateTo.EditValue!=null)
            {
                condition += " AND ppoi.DELIVERY_DATE<='" + ((DateTime)dtpDeliveryDateTo.EditValue).ToString("yyyy-MM-dd") + " 23:59:59' ";
            }
            if (!string.IsNullOrEmpty(cmbStatus.SelectedValue.ToString()))
            {
                condition += " AND ppoi.STATUS='" + cmbStatus.SelectedValue.ToString() + "' ";
            }
            if (!string.IsNullOrEmpty(txtProductCode.Text))
            {
                condition += " AND mp.PRODUCT_CODE='" + txtProductCode.Text + "' ";
            }
            return condition;
        }

        private void BindDataGrid()
        {
            string condition = GetCondition();
            string orderBy = "";
            DataTable dt = ProdPlanOrderItemBs.GetDtAllByCondition(condition, orderBy, "");
            gvMain.DataSource = dt;
        }
        #endregion

        #region Events
        protected override void OnShown(EventArgs e)
        {
            BindComobox();
            SetField();
            BindDataGrid();
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }

        protected override void Click_Confirm()
        {
            if (mSelectedProdPlanOrderItem == null)
            {
                MessageBox.Show("请选择工令单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            this.DialogResult = DialogResult.OK;
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mSelectedProdPlanOrderItem = new ProdPlanOrderItemEntity(dr);
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            if (mSelectedProdPlanOrderItem == null)
            {
                MessageBox.Show("请选择工令单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            this.DialogResult = DialogResult.OK;
        }

        private void txtProdPlanOrderCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainProductPlanOrderQuery frm = new frmMaintainProductPlanOrderQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedProdPlanOrder != null)
            {
                txtProdPlanOrderCode.Text = frm.SelectedProdPlanOrder.PlanOrderCode;
                txtProdPlanOrderCode.Tag = frm.SelectedProdPlanOrder;
            }
            else
            {
                txtProdPlanOrderCode.Text = "";
                txtProdPlanOrderCode.Tag = null;
            }
        }

        private void txtProductCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainProductQuery frm = new frmMaintainProductQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedProduct != null)
            {
                txtProductCode.Text = frm.SelectedProduct.ProductCode;
                txtProductCode.Tag = frm.SelectedProduct;
            }
            else
            {
                txtProductCode.Text = "";
                txtProductCode.Tag = null;
            }
        }
        #endregion



    }
}
